package eip.chapter4.guaranteed.impl;

import org.apache.log4j.Logger;
import org.mule.extras.client.MuleClient;

import eip.chapter4.guaranteed.GroceryOrder;
import eip.chapter4.guaranteed.GroceryOrderClient;
import eip.chapter4.guaranteed.util.JiBXHelper;

public class GroceryOrderClientImpl implements GroceryOrderClient{

	private static final Logger logger = Logger.getLogger(GroceryOrderClientImpl.class);
	private String target;
	
	public void sendOrder(GroceryOrder order) {
		try {
			String message = JiBXHelper.createOrderinXML(order); 
			MuleClient client = new MuleClient();
			if(logger.isInfoEnabled()) {
				logger.info("Sending message " + message + " to " + target);
			}
			client.dispatch(target, message, null);
		} catch (Exception e) {
			logger.error("Error sending message to target: " + target, e);
		} 
	}
	
	//---------------------------------------------
	// Simple setters and getters
	//---------------------------------------------

	public String getTarget() {
		return target;
	}

	public void setTarget(String target) {
		this.target = target;
	}

}
